<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="robot" content="index,follow">
<title>Module spf - Sprintf string formatter - Forth Foundation Library</title>
</head>
<body>
<h2>spf - Sprintf string formatter</h2>
<h3>Module Description</h3>
<p>The spf module implements a simplified version of C's sprintf function.
The words in this module expect a format string with specifiers (see
below for the format). For every specifier (except %% and %n) a
stack item is converted to the character representation and added to the
destination. All other characters are simply copied to the destination.
The float conversion uses the PRECISION value for precision.
<pre>
Format: %[flags][width][length]specifier
    Flags: 0      = Left-pads the number with zeros instead of spaces
           -      = Left justify the number
           +      = A positive number is preceded with a '+'
           space  = A positive number is preceded with a space
    Width: number = the minimum number of characters written
   Double: l      = the argument is interpreted as a double
Specifier: c      = format a character [char]
           d      = format a signed number [n or d]
           i      = format a signed number [n or d]
           o      = format a signed octal [n or d]
           s      = format a string [c-addr u]
           u      = format a unsigned number [u or ud]
           x      = format a unsigned hexadecimal number [u or ud]
           X      = format a unsigned hexadecimal number, capital letters
           p      = format a unsigned hexadecimal number [u or ud]
           e      = format a float number in scientific notation [r]
           E      = format a float number in scientific notation [r]
           n      = store the length of the string in [addr]
           q      = format a quoted string (non sprintf) [c-addr u]
           %      = write a '%' []
</pre>
</p>
<h3>Module Words</h3>
<dl>
</dl>
<h4>Sprintf words</h4>
<dl>
<dt><a name="word1"><b>spf-append</b>	( i*x j*r c-addr u str -- k*x l*r )</dt>
<dd>Convert the arguments i*x with the format string c-addr u and append the result to str</dd>
<dt><a name="word2"><b>spf-set</b>	( i*x j*r c-addr u str -- )</dt>
<dd>Convert the arguments i*x with the format string c-addr u and set the result in str</dd>
<dt><a name="word3"><b>spf"</b>	( "ccc&lt;quote&gt;" i*x j*r str -- )</dt>
<dd>Convert the arguments i*x with the format string and set the result in str</dd>
</dl>
<h3>Examples</h3>
<pre>
include ffl/spf.fs

str-new value spf1                                     \ Create string as destination

.( Format numbers                       : ) 
[DEFINED] represent [IF]
18E+0 15 15 10 8 1 s" %d %u %o %x %X %E" spf1 spf-set  \ Format the numbers
[ELSE]
      15 15 10 8 1 s" %d %u %o %x %X"    spf1 spf-set  \ Format the numbers
[THEN]

spf1 str-get type cr                                   \ Print the result

.( Format strings and characters        : )
char ! s" Hello" s" %s %c" spf1 spf-set                \ Format strings and characters

spf1 str-get type cr                                   \ Print result

.( Format numbers with signs            : )
-30 20 10 s" %+d % d %d" spf1 spf-set                  \ Format numbers with signs

spf1 str-get type cr                                   \ Print result

.( Leading and trailing zeros and spaces: )
-40 -30 20 10 s" %5d %-+5d %05d %5d" spf1 spf-set      \ Format numbers with leading and trailing spaces

spf1 str-get type cr

.( Format with parsing word             : )
time&amp;date spf1 spf" %04d-%02d-%2d %02d:%02d:%02d"      \ Format date &amp; time

spf1 str-get type cr                                   \ Print result

</pre>
<hr>
<div align="center">generated 24-Jul-2010 by <b>ofcfrth-0.10.0</b></div>
</body>
</html>
